﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CaiBaby.Common;
using CaiBaby.Entity;

namespace CaiBaby.DAL
{
	/// <summary>
    /// 管理员角色表数据访问层
	/// </summary>
	public class MangerRole
	{
        /// <summary>
        /// 增加一个角色
        /// </summary>
        /// <param name="mangerRoleInfo">角色实体类</param>
        /// <returns>返回角色ID</returns>
        public int Add(MangerRoleInfo mangerRoleInfo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO ma_MangerRole(");
            strSql.Append("Rname)");
            strSql.Append(" VALUES (");
            strSql.Append("@Rname)");
            strSql.Append(";SELECT @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Rname", SqlDbType.VarChar,32)};
            parameters[0].Value = mangerRoleInfo.Rname;

            object obj = SQLHelper.GetSingle(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 修改一个角色
        /// </summary>
        /// <param name="mangerRoleInfo">角色实体类</param>
        /// <returns>返回是否成功</returns>
        public bool Update(MangerRoleInfo mangerRoleInfo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE ma_MangerRole SET ");
            strSql.Append("Rname=@Rname) ");
            strSql.Append("WHERE RId=@RId ");
            SqlParameter[] parameters = {
                    new SqlParameter("@RId", SqlDbType.Int,4),
					new SqlParameter("@Rname", SqlDbType.VarChar,32)};
            parameters[0].Value = mangerRoleInfo.RId;
            parameters[1].Value = mangerRoleInfo.Rname;

            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一个角色
        /// </summary>
        /// <param name="rId">角色ID</param>
        /// <returns>返回是否成功</returns>
        public bool Delete(int rId)
        {
            string strSql1 = "DELETE FROM ma_MangerRole WHERE RId=@RId";
            string strSql2 = "DELETE FROM ma_RolePowerRelation WHERE RId=@RId";
            SqlParameter[] parameters = {
					new SqlParameter("@RId", SqlDbType.Int,4)};
            parameters[0].Value = rId;
            using (SqlTransaction tran = SQLHelper.BeginTransaction(ConfigHelper.CaiBabyDBConnStr))
            {
                try
                {
                    SQLHelper.ExecuteSql(tran, CommandType.Text, strSql1, parameters);
                    SQLHelper.ExecuteSql(tran, CommandType.Text, strSql2, parameters);
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    return false;
                }
            }
            return true;
        }
	}
}

